﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LSocket
{
    public class LSocketManager
    {
        internal static bool isQuit = false;
        static LNet _Net = null;
        #region 所有的外部接口都在这里
        /// <summary>
        /// 客户端上线时触发通知
        /// 这个时候记录下来客户端的LClient引用
        /// 后面就可以对这个客户端发送消息了
        /// </summary>
        public static event Action<LClient> OnAcceptClient;
        public static event Action<LMessage> OnReceived;
        /// <summary>
        /// 客户端掉线或者主动关闭
        /// </summary>
        public static event Action<LClient> OnClientClose;

        public static void Start() {
            isQuit = false;
            _Net = new LNet();

        }

        public static void Stop() {
            isQuit = true;
        }

        public static void Send(LClient client, byte[] cmd) {
            client.Send(cmd);
        }
        /// <summary>
        /// 对所有在线的客户端发送消息
        /// </summary>
        /// <param name="cmd"></param>
        public static void SendToAllClient(byte[] cmd) {
            foreach (LClient client in ClientManager.dicClients.Values) {
                client.Send(cmd);
            }
        }
        #endregion


        internal static void CallOnAcceptClient(LClient client) {
            if (OnAcceptClient != null) {
                OnAcceptClient(client);
            }
        }

        internal static void CallOnReceived(LMessage message) {
            if (OnReceived != null) {
                OnReceived(message);
            }
        }

        internal static void CallOnClientClose(LClient client) {
            if (OnClientClose != null) {
                OnClientClose(client);

            }
        }
    }
}
